﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yuans.BinaryConfig.Utils
{
    public class TextOutPutGadget
    {
        public static int tableWidth = 80;

        public static string GenLine()
        {
            return (new string('-', tableWidth));
        }

        public static string GenRowString(params string[] columns)
        {
            int width = (tableWidth - columns.Length) / columns.Length;
            string row = "|";

            foreach (string column in columns)
            {
                row += AlignCentre(column, width) + "|";
            }

            return row;
        }

        public static string AlignCentre(string text, int width)
        {
            text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

            if (string.IsNullOrEmpty(text))
            {
                return new string(' ', width);
            }
            else
            {
                return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
            }
        }
        public static string GenBriefHexByteString(byte[] rawBytes, int maxHexStringNum)
        {
            int i;
            int len = rawBytes.Length;
            StringBuilder sb = new StringBuilder();
            int rawBytesPtr;

            rawBytesPtr = 0;

            if (len > maxHexStringNum)
                len = maxHexStringNum;



            for (i = 0; i < len; i++)
            {
                 sb.Append(string.Format("0x{0:X2},", rawBytes[rawBytesPtr++]));
            }
            return sb.ToString();
        }
        public static string GenHexByteString(byte[] rawBytes, int cols)
        {
            int i,j;
            int rows;
            int remainder;
            int len = rawBytes.Length;
            StringBuilder sb = new StringBuilder();
            int rawBytesPtr;


            rows = len / cols;
            remainder = len % cols;
            rawBytesPtr = 0;

            for (i = 0; i < rows; i++)
            {
                for (j = 0; j < cols; j++)
                {
                    sb.Append(string.Format("0x{0:X2},", rawBytes[rawBytesPtr++]));
                }
                sb.Append(System.Environment.NewLine);
            }

            if (remainder != 0)
            {
                for (i = 0; i < remainder - 1; i++)
                {
                    sb.Append(string.Format("0x{0:X2},", rawBytes[rawBytesPtr++]));
                }
                sb.Append(string.Format("0x{0:X2},", rawBytes[rawBytesPtr]));
            }
            return sb.ToString();
        }
        public static string GenByteArray(byte[] rawBytes, int cols)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GenHexByteString(rawBytes, cols));
            sb.Append(System.Environment.NewLine);
            return sb.ToString();
        }
    }
}
